package com.kaily.image;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.kaily.image.share.share.CommonImageShareMerge;
import com.kaily.image.share.share.ICommonImageShareMerge;
import com.kaily.image.share.share.ImageShareInfo;
import com.kaily.image.utils.PermissionUtil;
import com.kaily.imagelib.MergeImage;


public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private Button genImgBtn;
    private Button shareImgBtn;
    private ImageView showImg;

    private ICommonImageShareMerge iCommonImageShareMerge;
    private ImageShareInfo imageShareInfo;
    private Bitmap bitmap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        genImgBtn = findViewById(R.id.gen_img_btn);
        shareImgBtn = findViewById(R.id.share_img_btn);
        showImg = findViewById(R.id.show_img);

        genImgBtn.setOnClickListener(this);
        shareImgBtn.setOnClickListener(this);

        //模拟器要合成图片的元素属性（坐标、颜色、大小等）
        iCommonImageShareMerge = new CommonImageShareMerge(this);
        //模拟要合成图片的元素值
        imageShareInfo = createShareContent();

    }

    /**
     * 创建要分享的内容
     *
     * @return
     */
    private ImageShareInfo createShareContent() {
        ImageShareInfo shareInfo = new ImageShareInfo();
        shareInfo.setAvater("https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=3502659583,3709665526&fm=26&gp=0.jpg");
        shareInfo.setImgUrl("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1561467411853&di=1bf5b0d7536a8bf0a75d299d37970395&imgtype=0&src=http%3A%2F%2Fimg4q.duitang.com%2Fuploads%2Fitem%2F201309%2F23%2F20130923175651_wu2tR.jpeg");
        shareInfo.setQrCodeUrl("https://www.jianshu.com/u/9f0c0dc41676");
        shareInfo.setUserName("激扬飞雪");
        shareInfo.setPrimaryText("我的简书");
        shareInfo.setSecondaryText("来自一个程序员的心声");
        return shareInfo;
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {

        if (PermissionUtil.REQUEST_CODE_PERMISSION_ADAPTER_STORAGE == requestCode) {
            if (PermissionUtil.getPermissionStorageResult(permissions, grantResults)) {
                shareImage(bitmap);
            } else {
                Toast.makeText(this, "此功能需要存储空间权限才能正常运作，请在“应用权限”中授予相应权限", Toast.LENGTH_SHORT).show();
            }
        }

        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    /**
     * 生成图片处理
     */
    private void genImageHandler() {
        iCommonImageShareMerge.mergeImage(imageShareInfo, new MergeImage.MergeCallBack() {
            @Override
            public Bitmap mergeProcess() {
                return null;
            }

            @Override
            public void callback(Bitmap bitmap) {
                if (bitmap != null) {
                    Toast.makeText(MainActivity.this, "图片生成成功", Toast.LENGTH_SHORT).show();
                    showImg.setImageBitmap(bitmap);
                } else {
                    Toast.makeText(MainActivity.this, "图片生成失败", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    /**
     * 分享图片处理
     */
    private void shareImageHandler() {
        iCommonImageShareMerge.mergeImage(imageShareInfo, new MergeImage.MergeCallBack() {
            @Override
            public Bitmap mergeProcess() {
                return null;
            }

            @Override
            public void callback(Bitmap bitmap) {
                if (bitmap != null) {
                    MainActivity.this.bitmap = bitmap;
                    Toast.makeText(MainActivity.this, "图片生成成功", Toast.LENGTH_SHORT).show();
                    if (PermissionUtil.permissionAdapterStorage(MainActivity.this)) {
                        shareImage(bitmap);
                    }
                } else {
                    Toast.makeText(MainActivity.this, "图片生成失败", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    /**
     * 调用系统分享面板
     *
     * @param bitmap
     */
    private void shareImage(Bitmap bitmap) {
        Uri uri = Uri.parse(MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, null, null));
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_SEND);//设置分享行为
        intent.setType("image/*");//设置分享内容的类型
        intent.putExtra(Intent.EXTRA_STREAM, uri);
        intent = Intent.createChooser(intent, "分享");
        startActivity(intent);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.gen_img_btn) {
            genImageHandler();
        } else if (id == R.id.share_img_btn) {
            shareImageHandler();
        }
    }
}
